package repository;

import java.sql.*;

/**
 * 数据库连接提供者
 */
public class ConnectionProvider {
    // private static final String URL =
    // "jdbc:aws-wrapper:mysql://localhost:3308/QQ";
    private static final String URL = "jdbc:mysql://localhost:3306/";
    private static final String USER = "root";
    private static final String PASSWORD = "root";
    private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
    private static Connection connection = null;

    static {
        try {
            Class.forName(DRIVER);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection() {
        try {
            if (connection == null) {
                return DriverManager.getConnection(URL, USER, PASSWORD);
            }
            return connection;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void closeConnection(ResultSet rs, Statement s, Connection con) {

        try {
            if (rs != null)
                rs.close();
            if (s != null)
                s.close();
            if (con != null)
                con.close();
            connection = null;
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    public static void closeConnection(Statement s, Connection con) {
        closeConnection(null, s, con);
        connection = null;
    }
}
